﻿using System;
using UnityEngine;
using UnityEngine.Rendering;

// Token: 0x0200004A RID: 74
[ExecuteInEditMode]
[AddComponentMenu("Alloy/Deferred Renderer Plus")]
[ImageEffectAllowedInSceneView]
[RequireComponent(typeof(Camera))]
public class AlloyDeferredRendererPlus : MonoBehaviour
{
	// Token: 0x06000290 RID: 656 RVA: 0x0000CAA4 File Offset: 0x0000ACA4
	private void Awake()
	{
		this.m_camera = base.GetComponent<Camera>();
	}

	// Token: 0x06000291 RID: 657 RVA: 0x0000CAB4 File Offset: 0x0000ACB4
	private void Reset()
	{
		this.ResetCommandBuffers();
	}

	// Token: 0x06000292 RID: 658 RVA: 0x0000CABC File Offset: 0x0000ACBC
	private void OnEnable()
	{
		this.ResetCommandBuffers();
	}

	// Token: 0x06000293 RID: 659 RVA: 0x0000CAC4 File Offset: 0x0000ACC4
	private void OnDisable()
	{
		this.DestroyCommandBuffers();
	}

	// Token: 0x06000294 RID: 660 RVA: 0x0000CACC File Offset: 0x0000ACCC
	private void OnDestroy()
	{
		this.DestroyCommandBuffers();
	}

	// Token: 0x06000295 RID: 661 RVA: 0x0000CAD4 File Offset: 0x0000ACD4
	public void Refresh()
	{
		bool enabled = this.SkinSettings.Enabled;
		bool flag = this.TransmissionSettings.Enabled || enabled;
		if (this.m_isTransmissionEnabled == flag && this.m_isScatteringEnabled == enabled)
		{
			this.RefreshProperties();
		}
		else
		{
			this.ResetCommandBuffers();
		}
	}

	// Token: 0x06000296 RID: 662 RVA: 0x0000CB2C File Offset: 0x0000AD2C
	private void RefreshProperties()
	{
		if (this.m_isTransmissionEnabled || this.m_isScatteringEnabled)
		{
			float x = (!this.m_isTransmissionEnabled) ? 0f : Mathf.GammaToLinearSpace(this.TransmissionSettings.Weight);
			Shader.SetGlobalVector("_DeferredTransmissionParams", new Vector4(x, this.TransmissionSettings.Falloff, this.TransmissionSettings.BumpDistortion, this.TransmissionSettings.ShadowWeight));
			if (this.m_isScatteringEnabled)
			{
				float num = 1f / Mathf.Tan(0.008726646f * this.m_camera.fieldOfView);
				float x2 = 0.15f * num;
				float y = 100f * num;
				Shader.SetGlobalVector("_DeferredBlurredNormalsParams", new Vector2(x2, y));
				Vector3 absorption = this.SkinSettings.Absorption;
				Vector3 aoColorBleed = this.SkinSettings.AoColorBleed;
				Shader.SetGlobalTexture("_DeferredSkinLut", this.SkinSettings.Lut);
				Shader.SetGlobalVector("_DeferredSkinParams", new Vector3(this.SkinSettings.Weight, 1f / this.SkinSettings.MaskCutoff, this.SkinSettings.BumpBlur));
				Shader.SetGlobalVector("_DeferredSkinTransmissionAbsorption", new Vector4(absorption.x, absorption.y, absorption.z, this.SkinSettings.Bias));
				Shader.SetGlobalVector("_DeferredSkinColorBleedAoWeights", new Vector4(aoColorBleed.x, aoColorBleed.y, aoColorBleed.z, this.SkinSettings.Scale));
			}
		}
	}

	// Token: 0x06000297 RID: 663 RVA: 0x0000CCBC File Offset: 0x0000AEBC
	private void ResetCommandBuffers()
	{
		this.m_isScatteringEnabled = this.SkinSettings.Enabled;
		this.m_isTransmissionEnabled = (this.TransmissionSettings.Enabled || this.m_isScatteringEnabled);
		if (this.SkinSettings.Lut == null)
		{
			this.SkinSettings.Lut = this.SkinLut;
		}
		this.DestroyCommandBuffers();
		if ((this.m_isTransmissionEnabled || this.m_isScatteringEnabled) && this.m_camera != null && this.DeferredTransmissionBlit != null)
		{
			int nameID = Shader.PropertyToID("_DeferredPlusBuffer");
			this.m_deferredTransmissionBlitMaterial = new Material(this.DeferredTransmissionBlit);
			this.m_deferredTransmissionBlitMaterial.hideFlags = HideFlags.HideAndDontSave;
			this.m_copyTransmission = new CommandBuffer();
			this.m_copyTransmission.name = "AlloyCopyTransmission";
			if (!this.m_isScatteringEnabled)
			{
				this.m_copyTransmission.GetTemporaryRT(nameID, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
				this.m_copyTransmission.Blit(BuiltinRenderTextureType.CameraTarget, nameID, this.m_deferredTransmissionBlitMaterial);
				this.m_copyTransmission.ReleaseTemporaryRT(nameID);
			}
			else if (this.DeferredBlurredNormals != null)
			{
				int width = this.m_camera.pixelWidth / 2;
				int height = this.m_camera.pixelHeight / 2;
				int nameID2 = Shader.PropertyToID("_DeferredBlurredNormalPingBuffer");
				int nameID3 = Shader.PropertyToID("_DeferredBlurredNormalPongBuffer");
				this.m_copyTransmission.SetGlobalTexture("_DeferredTransmissionBuffer", BuiltinRenderTextureType.CameraTarget);
				this.m_deferredBlurredNormalsMaterial = new Material(this.DeferredBlurredNormals);
				this.m_deferredBlurredNormalsMaterial.hideFlags = HideFlags.HideAndDontSave;
				this.m_renderBlurredNormals = new CommandBuffer();
				this.m_renderBlurredNormals.name = "AlloyBlurNormals";
				this.m_renderBlurredNormals.GetTemporaryRT(nameID, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
				this.m_renderBlurredNormals.GetTemporaryRT(nameID2, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
				this.m_renderBlurredNormals.GetTemporaryRT(nameID3, width, height, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
				this.m_renderBlurredNormals.Blit(BuiltinRenderTextureType.GBuffer2, nameID2, this.m_deferredBlurredNormalsMaterial, 0);
				this.m_renderBlurredNormals.Blit(nameID2, nameID3, this.m_deferredBlurredNormalsMaterial, 1);
				this.m_renderBlurredNormals.Blit(nameID3, nameID2, this.m_deferredBlurredNormalsMaterial, 2);
				this.m_renderBlurredNormals.Blit(nameID2, nameID, this.m_deferredBlurredNormalsMaterial, 3);
				this.m_renderBlurredNormals.ReleaseTemporaryRT(nameID);
				this.m_renderBlurredNormals.ReleaseTemporaryRT(nameID2);
				this.m_renderBlurredNormals.ReleaseTemporaryRT(nameID3);
				this.m_camera.depthTextureMode |= DepthTextureMode.Depth;
				this.m_camera.AddCommandBuffer(CameraEvent.BeforeLighting, this.m_renderBlurredNormals);
			}
			this.m_camera.AddCommandBuffer(CameraEvent.AfterGBuffer, this.m_copyTransmission);
		}
		this.RefreshProperties();
	}

	// Token: 0x06000298 RID: 664 RVA: 0x0000CF94 File Offset: 0x0000B194
	private void DestroyCommandBuffers()
	{
		if (this.m_copyTransmission != null)
		{
			this.m_camera.RemoveCommandBuffer(CameraEvent.AfterGBuffer, this.m_copyTransmission);
		}
		if (this.m_renderBlurredNormals != null)
		{
			this.m_camera.RemoveCommandBuffer(CameraEvent.BeforeLighting, this.m_renderBlurredNormals);
		}
		if (this.m_deferredTransmissionBlitMaterial != null)
		{
			UnityEngine.Object.DestroyImmediate(this.m_deferredTransmissionBlitMaterial);
		}
		if (this.m_deferredBlurredNormalsMaterial != null)
		{
			UnityEngine.Object.DestroyImmediate(this.m_deferredBlurredNormalsMaterial);
		}
		this.m_copyTransmission = null;
		this.m_renderBlurredNormals = null;
		this.m_deferredTransmissionBlitMaterial = null;
		this.m_deferredBlurredNormalsMaterial = null;
	}

	// Token: 0x040001C2 RID: 450
	private const float c_blurWdith = 0.15f;

	// Token: 0x040001C3 RID: 451
	private const float c_blurDepthDifferenceMultiplier = 100f;

	// Token: 0x040001C4 RID: 452
	private const string c_copyTransmissionBufferName = "AlloyCopyTransmission";

	// Token: 0x040001C5 RID: 453
	private const string c_blurNormalsBufferName = "AlloyBlurNormals";

	// Token: 0x040001C6 RID: 454
	private const CameraEvent c_copyTransmissionEvent = CameraEvent.AfterGBuffer;

	// Token: 0x040001C7 RID: 455
	private const CameraEvent c_blurNormalsEvent = CameraEvent.BeforeLighting;

	// Token: 0x040001C8 RID: 456
	public AlloyDeferredRendererPlus.SkinSettingsData SkinSettings = new AlloyDeferredRendererPlus.SkinSettingsData
	{
		Enabled = true,
		Weight = 1f,
		MaskCutoff = 0.1f,
		Bias = 0f,
		Scale = 1f,
		BumpBlur = 0.7f,
		Absorption = new Vector3(-8f, -40f, -64f),
		AoColorBleed = new Vector3(0.4f, 0.15f, 0.13f)
	};

	// Token: 0x040001C9 RID: 457
	public AlloyDeferredRendererPlus.TransmissionSettingsData TransmissionSettings = new AlloyDeferredRendererPlus.TransmissionSettingsData
	{
		Enabled = true,
		Weight = 1f,
		ShadowWeight = 0.5f,
		BumpDistortion = 0.05f,
		Falloff = 1f
	};

	// Token: 0x040001CA RID: 458
	[HideInInspector]
	public Texture2D SkinLut;

	// Token: 0x040001CB RID: 459
	[HideInInspector]
	public Shader DeferredTransmissionBlit;

	// Token: 0x040001CC RID: 460
	[HideInInspector]
	public Shader DeferredBlurredNormals;

	// Token: 0x040001CD RID: 461
	private Material m_deferredTransmissionBlitMaterial;

	// Token: 0x040001CE RID: 462
	private Material m_deferredBlurredNormalsMaterial;

	// Token: 0x040001CF RID: 463
	private Camera m_camera;

	// Token: 0x040001D0 RID: 464
	private bool m_isTransmissionEnabled;

	// Token: 0x040001D1 RID: 465
	private bool m_isScatteringEnabled;

	// Token: 0x040001D2 RID: 466
	private CommandBuffer m_copyTransmission;

	// Token: 0x040001D3 RID: 467
	private CommandBuffer m_renderBlurredNormals;

	// Token: 0x0200004B RID: 75
	[Serializable]
	public struct SkinSettingsData
	{
		// Token: 0x040001D4 RID: 468
		public bool Enabled;

		// Token: 0x040001D5 RID: 469
		public Texture2D Lut;

		// Token: 0x040001D6 RID: 470
		[Range(0f, 1f)]
		public float Weight;

		// Token: 0x040001D7 RID: 471
		[Range(0.01f, 1f)]
		public float MaskCutoff;

		// Token: 0x040001D8 RID: 472
		[Range(0f, 1f)]
		public float Bias;

		// Token: 0x040001D9 RID: 473
		[Range(0f, 1f)]
		public float Scale;

		// Token: 0x040001DA RID: 474
		[Range(0f, 1f)]
		public float BumpBlur;

		// Token: 0x040001DB RID: 475
		public Vector3 Absorption;

		// Token: 0x040001DC RID: 476
		public Vector3 AoColorBleed;
	}

	// Token: 0x0200004C RID: 76
	[Serializable]
	public struct TransmissionSettingsData
	{
		// Token: 0x040001DD RID: 477
		public bool Enabled;

		// Token: 0x040001DE RID: 478
		[Range(0f, 1f)]
		public float Weight;

		// Token: 0x040001DF RID: 479
		[Range(0f, 1f)]
		public float ShadowWeight;

		// Token: 0x040001E0 RID: 480
		[Range(0f, 1f)]
		[Tooltip("Amount that the transmission is distorted by surface normals.")]
		public float BumpDistortion;

		// Token: 0x040001E1 RID: 481
		[MinValue(1f)]
		public float Falloff;
	}
}
